#include <iostream>

using namespace std;

//https://www.nowcoder.com/practice/f78a359491e64a50bce2d89cff857eb6?tpId=13&tqId=11199&ru=/exam/oj

class Solution
{
public:
    int LastRemaining_Solution(int n, int m)
    {
        int dp = 0;
        for (int i = 2; i <= n; i++) dp = (dp + m) % i;
        return dp;
    }
};

void test01()
{

}

int main()
{
    test01();
    return 0;
}